package com.axter.libs.photo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.axter.libs.photo.R;

/**
 * @author axter
 */
public class RectImageView extends ImageView {
    private int foregroundColor = Color.TRANSPARENT;
    private boolean isShowForegroundColor = false;

    public RectImageView(Context context) {
        super(context);
    }

    public RectImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RectImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RectImageView);
        foregroundColor = array.getColor(R.styleable.RectImageView_cpvForegroundColor, foregroundColor);
        isShowForegroundColor = array.getBoolean(R.styleable.RectImageView_cpvIsShowForegroundColor, isShowForegroundColor);
        array.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (foregroundColor != 0 && isShowForegroundColor) {
            canvas.drawColor(foregroundColor);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    public boolean isShowForegroundColor() {
        return isShowForegroundColor;
    }

    public void showForegroundColor(boolean showForegroundColor, boolean isRefresh) {
        isShowForegroundColor = showForegroundColor;
        if (isRefresh) {
            invalidate();
        }
    }
}
